<!DOCTYPE html>
<html>
<head>
    <title></title>
    <meta charset="UTF-8">
    <link rel="stylesheet" type="text/css" href="../../Css/bootstrap.css"/>
    <link rel="stylesheet" type="text/css" href="../../Css/bootstrap-responsive.css"/>
    <link rel="stylesheet" type="text/css" href="../../Css/style.css"/>
    <script type="text/javascript" src="../../Js/jquery.js"></script>
    <script type="text/javascript" src="../../Js/bootstrap.js"></script>
    <script type="text/javascript" src="../../Js/ckform.js"></script>
    <script type="text/javascript" src="../../Js/common.js"></script>
    <script>

        let password = null;
        function validatanewPwd(){
            let newpass = $("#newpass").val();
            let renewpass=$("#renewpass").val();
            let msg2 = $("#repassMsg");
            if (renewpass!=newpass){
                msg2.html("两次输入密码不一样").css("color", "red");
                return false;
            }else {
                msg2.html("ok").css("color", "green");
                return true;
            }
        }
        function validataPwd() {
            let newpass = $("#newpass").val();
            let msg = $("#passMsg");
            if (newpass == "") {
                msg.html("密码不能为空").css("color", "red");
                return false;
            } else if (newpass.length < 8) {
                msg.html("密码长度必须大于等于8位").css("color", "red");
                return false;
            } else if (newpass.length >= 8 && /\d/.test(newpass)) {
                msg.html("weak").css("color", "red");
                if (newpass.length >= 8 && /\d/.test(newpass) && /[a-z]/.test(newpass)) {
                    msg.html("middle").css("color", "orange");
                    if (newpass.length >= 8 && /\d/.test(newpass) && /[a-z]/.test(newpass) && /[A-Z]/.test(newpass)) {
                        msg.html("strong").css("color", "green");
                        return true;
                    }
                    return true;
                }
                return false;
            }
        }

        function validataOriPwd() {
            let newpass = $("#oripass").val();
            let msg = $("#oripassMsg");
            if (newpass == "") {
                msg.html("密码不能为空").css("color", "red");
                return false;
            } else if (newpass.length < 8) {
                msg.html("密码长度必须大于等于8位").css("color", "red");
                return false;
            } else if (newpass.length >= 8 && /\d/.test(newpass)) {
                msg.html("weak").css("color", "red");
                if (newpass.length >= 8 && /\d/.test(newpass) && /[a-z]/.test(newpass)) {
                    msg.html("middle").css("color", "orange");
                    if (newpass.length >= 8 && /\d/.test(newpass) && /[a-z]/.test(newpass) && /[A-Z]/.test(newpass)) {
                        msg.html("strong").css("color", "green");
                        return true;
                    }
                    return true;
                }
                return false;
            }
        }

        $(function () {
            $("#newpass").blur(function () {
                validataPwd();
            });
            $("#renewpass").blur(function () {
                validatanewPwd();
            });
            $("#oripass").blur(function () {
                validataOriPwd();
            });
        });

        $(function () {
                $.getJSON("/userinfo.do", null, function (vo) {
                    password = vo.obj.uPassword;
                });

                $("#save").click(function () {
                    let oripass = $("#oripass").val();
                    let newpass = $("#newpass").val();
                    let renewpass = $("#renewpass").val();
                    if (oripass == null) {
                        alert("请输入原密码");
                    } else {
                        if (oripass != password) {
                            console.log(oripass);
                            alert("原密码错误");
                        } else {
                            if (newpass == null || renewpass == null) {
                                alert("请输入新密码，并重复一次");
                            } else {
                                if (newpass != renewpass) {
                                    alert("两次输入不相同");
                                } else {
                                    $.getJSON("/user/updatePassword.do", {newpass: newpass}, function (vo) {
                                        if (vo.code == 200) {
                                            alert("修改成功");
                                            window.parent.location.href = "/pages/login.html";
                                        } else {
                                            alert(vo.msg);
                                        }
                                    });
                                }
                            }
                        }
                    }
                });
            }
        );
    </script>
</head>
<body>
<form class="form-inline definewidth m20" action="" method="get">
    <table>
        <tr>
            <td align="right">原密码:</td>
            <td><input id="oripass" type="password"/><span id="oripassMsg"></span></td>
        </tr>
        <tr>
            <td align="right">新密码:</td>
            <td><input id="newpass" type="password"/><span id="passMsg"></span></td>
        </tr>
        <tr>
            <td align="right">确认密码:</td>
            <td><input id="renewpass" type="password"/><span id="repassMsg"></span></td>
        </tr>
        <tr>
            <td colspan="2" align="center"><br/>
                <input id="save" type="button" value="保存" class="btn btn-primary"/>
            </td>
        </tr>
    </table>
</form>
</body>
</html>